using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using Catastrophe.BasePC.UI.SystemIni.BaseData;
using Catastrophe.BasePC.UI.Model;
using Catastrophe.BasePC.UI.SystemIni;
using Catastrophe.Common.AttributeEx;
using SqlSugarEx;
using SqlSugarEx.Ret;
using SqlSugarEx.Utilities;

namespace Catastrophe.BasePC.UI.DbDal
{
	public class SysDictionaryDal : ISysDictionaryDal
	{
		private readonly SqlSugarClientEx _dbSugar;

		public SysDictionaryDal()
		{
			this._dbSugar = DbUtility.GetSugar(Catastrophe.Initialize.SystemIni.FileConfigIni.GetDbAppConfig, isAutoCloseCon: true);
		}

		public List<SysDictionaryEntity> QueryEntities(string assembleName, string dicName)
		{
			return (from x in this._dbSugar.Queryable<SysDictionaryEntity>().WhereIF(!string.IsNullOrWhiteSpace(assembleName), (SysDictionaryEntity x) => x.Assembly == assembleName).WhereIF(!string.IsNullOrWhiteSpace(dicName), (SysDictionaryEntity x) => x.DicName.Contains(dicName))
				orderby x.Assembly
				orderby x.DicName
				select x).ToList();
		}

		public DbOperateRet AddEntitySingle(SysDictionaryEntity entity)
		{
			DbOperateRet dbOperateRet = new DbOperateRet();
			int num = (from x in this._dbSugar.Queryable<SysDictionaryEntity>()
				where x.DicName == entity.DicName
				where x.DicKey == entity.DicKey || x.DicValue == entity.DicValue
				select x).Count();
			if (num > 0)
			{
				DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(11, 1);
				defaultInterpolatedStringHandler.AppendLiteral("已经存在当前信息，数量");
				defaultInterpolatedStringHandler.AppendFormatted(num);
				dbOperateRet.Message = defaultInterpolatedStringHandler.ToStringAndClear();
				return dbOperateRet;
			}
			int num2 = this._dbSugar.Insertable(entity).ExecuteCommand();
			dbOperateRet.Success = num2 > 0;
			return dbOperateRet;
		}

		public DbOperateRet EditEntitySingle(SysDictionaryEntity entity)
		{
			DbOperateRet dbOperateRet = new DbOperateRet();
			int num = (from x in this._dbSugar.Queryable<SysDictionaryEntity>()
				where x.DicName == entity.DicName
				where x.DicKey == entity.DicKey || x.DicValue == entity.DicValue
				where x.Id != entity.Id
				select x).Count();
			if (num > 0)
			{
				DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(11, 1);
				defaultInterpolatedStringHandler.AppendLiteral("已经存在当前信息，数量");
				defaultInterpolatedStringHandler.AppendFormatted(num);
				dbOperateRet.Message = defaultInterpolatedStringHandler.ToStringAndClear();
				return dbOperateRet;
			}
			int num2 = this._dbSugar.Updateable(entity).ExecuteCommand();
			dbOperateRet.Success = num2 > 0;
			return dbOperateRet;
		}

		public int DeleteSelect(List<long> pkBuff)
		{
			return this._dbSugar.Deleteable<SysDictionaryEntity>().In(pkBuff).ExecuteCommand();
		}

		public T ConvertToCusEntity<T>(string assembleName = "", string dicName = "") where T : class, new()
		{
			if (string.IsNullOrWhiteSpace(assembleName))
			{
				assembleName = typeof(T).Assembly.GetName().Name;
			}
			List<SysDictionaryEntity> list = this.QueryEntities(assembleName, dicName);
			if (list.Count == 0)
			{
				return null;
			}
			T val = new T();
			PropertyInfo[] properties = typeof(T).GetProperties();
			PropertyInfo[] array = properties;
			foreach (PropertyInfo propertyInfo in array)
			{
				DescribeInfoAttribute describeInfo = BuildAttribute.GetPropertyDescribe(propertyInfo);
				if (describeInfo == null)
				{
					continue;
				}
				List<SysDictionaryEntity> list2 = list.Where((SysDictionaryEntity x) => x.DicName == describeInfo.Description).ToList();
				if (list2.Count == 0)
				{
					continue;
				}
				if (propertyInfo.PropertyType == typeof(SysDictionaryEntity))
				{
					propertyInfo.SetValue(val, list2.First());
				}
				else if (propertyInfo.PropertyType == typeof(List<SysDictionaryEntity>))
				{
					propertyInfo.SetValue(val, list2);
				}
				else if (propertyInfo.PropertyType == typeof(Dictionary<string, string>))
				{
					Dictionary<string, string> dictionary = new Dictionary<string, string>();
					foreach (SysDictionaryEntity item in list2)
					{
						if (!dictionary.ContainsKey(item.DicKey))
						{
							dictionary.Add(item.DicKey, item.DicValue);
						}
					}
					propertyInfo.SetValue(val, dictionary);
				}
				else if (propertyInfo.PropertyType == typeof(List<string>))
				{
					propertyInfo.SetValue(val, list2.Select((SysDictionaryEntity x) => x.DicValue).ToList());
				}
			}
			return val;
		}

		/// <summary>
		/// 按key查询
		/// </summary>
		/// <exception cref="NotImplementedException"></exception>
        public List<SysDictionaryEntity> QueryByKey(string key)
        {
			return _dbSugar.Queryable<SysDictionaryEntity>().Where(x => x.DicKey == key).ToList();
        }

		/// <summary>
		/// 删除
		/// </summary>
		/// <param name="name"></param>
		/// <param name="type"></param>
		/// <returns></returns>
        public int DeleteByName(string name, string type)
        {
			return _dbSugar.Deleteable<SysDictionaryEntity>().Where(x => x.DicKey == type && x.DicName == name).ExecuteCommand();
        }
    }
}
